package com.wardrobe.soa.backend.agreement;


import com.google.common.collect.Maps;
import com.wardrobe.domain.agreement.AgreementBean;
import com.wardrobe.domain.agreement.AgreementShowBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/7.
 */
@RestController
@RequestMapping("/backend/agreement")
public class BackAgreementController {
    private static final Logger logger = LoggerFactory.getLogger(BackAgreementController.class);

    @Autowired
    private BackAgreementService agreementService;

    @RequestMapping("/agreementList")
    public Map<String, Object> agreementList() {
        Map<String, Object> result = Maps.newHashMap();
        List<AgreementShowBean> list = agreementService.getAgreementList();
        result.put("list", list);
        return result;
    }
    @RequestMapping("/addAgreement")
    public Map<String,Object> addAgreement(AgreementBean agreementBean){
        Map<String,Object> result = Maps.newHashMap();
        try {
            AgreementShowBean showBean = agreementService.getOneAgreement(agreementBean.getType());
            if (showBean!=null){
                result.put("status",2);
                result.put("mess","已添加过,请重新选择类型");
                return result;
            }
            int status = agreementService.addAgreement(agreementBean);
            if(status==1){
                result.put("status",status);
                result.put("mess","添加成功");
            }else{
                result.put("status",2);
                result.put("mess","修添加失败");
            }
            return result;
        }catch (Exception e){
            result.put("status",2);
            result.put("mess","添加失败");
            logger.error("addAgreement error",e);
        }
        return result;
    }
    @RequestMapping("/modifyAgreement")
    public Map<String,Object> modifyAgreement(AgreementBean fromBean){
        Map<String,Object> result = Maps.newHashMap();
        try {
            int status = agreementService.modifyAgreement(fromBean);
            if(status==1){
                result.put("status",status);
                result.put("mess","修改成功");
            }else{
                result.put("status",2);
                result.put("mess","修改失败");
            }
            return result;
        }catch (Exception e){
            result.put("status",2);
            result.put("mess","修改失败");
            logger.error("modifyAgreement error",e);
        }
        return result;
    }

    @RequestMapping("/removeAgrementById")
    public int removeAgrementById(int id){
        return agreementService.removeAgrementById(id);
    }
    @RequestMapping("/getOneAgreement")
    public AgreementShowBean getOneAgreement(int type){
        AgreementShowBean oneAgreement = agreementService.getOneAgreement(type);
        oneAgreement.setCreateTime(oneAgreement.getCreateTime().substring(0, oneAgreement.getCreateTime().length() - 2));
        return oneAgreement;
    }
}
